package com.ruoyi.project.modules.interfaceinfo.controller;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.modules.businessSystem.domain.BusinessSystem;
import com.ruoyi.project.modules.businessSystem.service.IBusinessSystemService;
import com.ruoyi.project.modules.domainInfo.domain.DomainInfo;
import com.ruoyi.project.modules.domainInfo.service.IDomainInfoService;
import com.ruoyi.project.modules.interfaceGenerator.service.IInterfaceGeneratorService;
import com.ruoyi.project.modules.interfaceModel.domain.InterfaceModel;
import com.ruoyi.project.modules.interfaceModel.service.IInterfaceModelService;
import com.ruoyi.project.modules.interfaceObject.domain.InterfaceObject;
import com.ruoyi.project.modules.interfaceObject.service.IInterfaceObjectService;
import com.ruoyi.project.modules.interfaceTestLog.service.IInterfaceTestLogService;
import com.ruoyi.project.modules.interfaceinfo.domain.VirtualUsage;
import com.ruoyi.project.modules.interfaceinfo.service.IVirtualUsageService;
import com.ruoyi.project.modules.interfaceinfo.vo.InterfaceParam;
import com.ruoyi.project.modules.parameter.domain.InterfaceParamsFieldRelation;
import com.ruoyi.project.modules.parameter.service.IInterfaceParamsFieldRelationService;
import com.ruoyi.project.modules.product.domain.ProductInfo;
import com.ruoyi.project.modules.product.mapper.ProductInfoMapper;
import com.ruoyi.project.modules.product.service.IProductInfoService;
import com.ruoyi.project.modules.productVersionObejct.domain.ProductVersionObejct;
import com.ruoyi.project.modules.productVersionObejct.mapper.ProductVersionObejctMapper;
import com.ruoyi.project.modules.productVersionObejct.service.IProductVersionObejctService;
import com.ruoyi.project.modules.table.domain.TableInfo;
import com.ruoyi.project.modules.table.service.ITableInfoService;
import com.ruoyi.project.modules.tableObject.domain.TableObject;
import com.ruoyi.project.modules.tableObject.service.ITableObjectService;
import com.ruoyi.project.modules.usage.domain.Usage;
import com.ruoyi.system.service.ISysDictTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.interfaceinfo.domain.InterfaceInfo;
import com.ruoyi.project.modules.interfaceinfo.service.IInterfaceInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 实体用况Controller
 *
 * @author smallrain
 * @date 2021-10-29
 */
@Controller
@RequestMapping("/interface/interface")
public class InterfaceInfoController extends BaseController {
    private String prefix = "interface/interface";

    @Autowired
    private IInterfaceInfoService interfaceInfoService;

    @Autowired
    private IInterfaceModelService interfaceModelService;

    @Autowired
    private ISysDictTypeService sysDictTypeService;

    @Resource
    private ProductInfoMapper productInfoMapper;

    @Resource
    private ProductVersionObejctMapper productVersionObejctMapper;

    @RequiresPermissions("interface:interface:view")
    @GetMapping()
    public String interfaceinfo(ModelMap modelMap) {
        List<DomainInfo> domainInfoList = this.domainInfoService.selectDomainInfoList(new DomainInfo());
        modelMap.put("domainInfoList", domainInfoList);

        InterfaceModel interfaceModel = new InterfaceModel();
        interfaceModel.setModelType("1");
        List<InterfaceModel> fieldModelList = interfaceModelService.selectInterfaceModelList2(interfaceModel);
        interfaceModel.setModelType("2");
        List<InterfaceModel> selfFieldModelList = interfaceModelService.selectInterfaceModelList2(interfaceModel);
        interfaceModel.setModelType("3");
        List<InterfaceModel> aliasZhNameList = interfaceModelService.selectInterfaceModelList2(interfaceModel);
        interfaceModel.setModelType("4");
        List<InterfaceModel> aliasEnNameList = interfaceModelService.selectInterfaceModelList2(interfaceModel);
        modelMap.put("fieldModelList", fieldModelList);
        modelMap.put("selfFieldModelList", selfFieldModelList);
        modelMap.put("aliasZhNameList", aliasZhNameList);
        modelMap.put("aliasEnNameList", aliasEnNameList);

        List<SysDictData> dictCategoryList = new ArrayList<>();
        List<SysDictData> dataDictionary = this.sysDictTypeService.selectDictDataByType("dataDictionary");
        for (SysDictData sysDictData : dataDictionary) {
            //if (!"1".equals(sysDictData.getDictValue()) && !"2".equals(sysDictData.getDictValue())) {
            if (!"1".equals(sysDictData.getDictValue())) {
                dictCategoryList.add(sysDictData);
            }
        }
        modelMap.put("dictCategoryList", dictCategoryList);

        ProductInfo productInfo = new ProductInfo();
        productInfo.setVersionType("1");
        List<ProductInfo> productInfos = this.productInfoService.selectProductInfoList(productInfo);
        modelMap.put("productInfoList", productInfos);

        return prefix + "/interface";
    }

    /**
     * 查询实体用况列表
     */
    @RequiresPermissions("interface:interface:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(InterfaceInfo interfaceInfo) {
        //startPage();
        interfaceInfo.setUserId(ShiroUtils.getSysUser().getUserId());
        interfaceInfo.setFlag(this.getFlag());
        if ("-1".equals(interfaceInfo.getLifeCycle())) {
            interfaceInfo.setLifeCycle(null);
        }
        List<InterfaceInfo> list = interfaceInfoService.selectInterfaceInfoList(interfaceInfo);
        return getDataTable(list);
    }

    /**
     * 导出实体用况列表
     */
    @RequiresPermissions("interface:interface:export")
    @Log(title = "实体用况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(InterfaceInfo interfaceInfo) {
        String fileName = "接口mock数据" + System.currentTimeMillis() + ".zip";
        String downloadPath = RuoYiConfig.getDownloadPath() + fileName;
        File desc = new File(downloadPath);
        if (!desc.getParentFile().exists()) {
            desc.getParentFile().mkdirs();
        }
        ZipOutputStream zipOut = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(downloadPath);
            zipOut = new ZipOutputStream(fileOutputStream);
            List<InterfaceInfo> list = interfaceInfoService.selectInterfaceInfoList(interfaceInfo);
            if (CollectionUtil.isNotEmpty(list)) {
                for (InterfaceInfo info : list) {
                    zipOut.putNextEntry(new ZipEntry(info.getInterfaceName() + ".json"));
                    String mockData = this.getMockData(info);
                    zipOut.write(mockData.getBytes());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zipOut != null) {
                try {
                    zipOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return AjaxResult.success(fileName);
    }

    private String getMockData(InterfaceInfo info) {
        String fbType = info.getFbType();
        if (fbType.equals("3")) {
            fbType = "4";
        }
        Map<String, Object> mock = new HashMap<>();
        mock.put("errCode", "1");
        mock.put("message", "获取成功");
        List<InterfaceParam> responseList = this.interfaceInfoService.paramList(fbType, info.getId(), "2");
        if (CollectionUtil.isNotEmpty(responseList)) {
            if (responseList.size() == 2) {
                InterfaceParam interfaceParam1 = responseList.get(0);
                InterfaceParam interfaceParam2 = responseList.get(1);
                if (StringUtils.isNotEmpty(interfaceParam1.getNodeType()) && StringUtils.isNotEmpty(interfaceParam2.getNodeType())) {
                    if (interfaceParam1.getNodeType().equals("F") && interfaceParam2.getNodeType().equals("Z")) {
                        List<InterfaceParam> children = interfaceParam1.getChildren();
                        if (children == null) {
                            children = new ArrayList<>();
                            interfaceParam1.setChildren(children);
                        }
                        children.add(interfaceParam2);
                        responseList = Arrays.asList(interfaceParam1);
                    } else if (interfaceParam1.getNodeType().equals("Z") && interfaceParam2.getNodeType().equals("F")) {
                        List<InterfaceParam> children = interfaceParam2.getChildren();
                        if (children == null) {
                            children = new ArrayList<>();
                            interfaceParam2.setChildren(children);
                        }
                        children.add(interfaceParam1);
                        responseList = Arrays.asList(interfaceParam2);
                    }
                }
            }

            if (responseList.size() == 1) {
                InterfaceParam interfaceParam = responseList.get(0);
                if (CollectionUtil.isEmpty(interfaceParam.getChildren())) {
                    mock.put("data", interfaceParam.getMockData());
                } else {
                    List<InterfaceParam> children = interfaceParam.getChildren();
                    if (children.size() == 1) {
                        mock.put("data", Collections.singletonList(children.get(0).getMockData()));
                    } else {
                        List<Map<String, Object>> data = new ArrayList<>();
                        mock.put("data", data);
                        Map<String, Object> map = new HashMap<>();
                        for (InterfaceParam childInterfaceParam : children) {
                            if (CollectionUtil.isNotEmpty(childInterfaceParam.getChildren())) {
                                List<Map<String, Object>> child = new ArrayList<>();
                                Map<String, Object> mapChild = new HashMap<>();
                                for (InterfaceParam interfaceParamChild : childInterfaceParam.getChildren()) {
                                    mapChild.put(interfaceParamChild.getFieldEnName(), interfaceParamChild.getMockData());
                                }
                                child.add(mapChild);
                                map.put(childInterfaceParam.getFieldEnName(), child);
                            } else {
                                map.put(childInterfaceParam.getFieldEnName(), childInterfaceParam.getMockData());
                            }
                        }
                        data.add(map);
                    }
                }
            } else {
                Map<String, Object> data = new HashMap<>();
                mock.put("data", data);
                for (InterfaceParam interfaceParam : responseList) {
                    if (interfaceParam.getObjectType().equals("1")) {
                        data.put(interfaceParam.getFieldEnName(), interfaceParam.getMockData());
                    } else {
                        List<Object> dataMap = new ArrayList<>();
                        if (CollectionUtil.isNotEmpty(interfaceParam.getChildren())) {
                            if (interfaceParam.getChildren().size() > 1) {
                                Map<String, Object> child = new HashMap<>();
                                dataMap.add(child);
                                for (InterfaceParam interfaceParamChild : interfaceParam.getChildren()) {
                                    child.put(interfaceParamChild.getFieldEnName(), interfaceParamChild.getMockData());
                                }
                            } else {
                                dataMap.add(interfaceParam.getChildren().get(0).getMockData());
                            }
                        }
                        data.put(interfaceParam.getFieldEnName(), dataMap);
                    }
                }
            }
        }
        return JSONObject.toJSONString(mock);
    }

    @GetMapping("/setParam")
    public String setParam(@RequestParam Long id, ModelMap mmap) {
        mmap.put("interfaceInfo", this.interfaceInfoService.selectInterfaceInfoById(id));
        return prefix + "/setParam";
    }

    @Autowired
    private IDomainInfoService domainInfoService;
    /**
     * 新增实体用况
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        DomainInfo domainInfo = new DomainInfo();
        List<DomainInfo> domainInfos = domainInfoService.selectDomainInfoList(domainInfo);
        modelMap.put("domainInfoList", domainInfos);
        return prefix + "/add";
    }

    /**
     * 新增保存实体用况
     */
    @RequiresPermissions("interface:interface:add")
    @Log(title = "实体用况", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(InterfaceInfo interfaceInfo) {
        return toAjax(interfaceInfoService.insertInterfaceInfo(interfaceInfo));
    }

    /**
     * 修改实体用况
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        InterfaceInfo interfaceInfo = interfaceInfoService.selectInterfaceInfoById(id);
        mmap.put("interfaceInfo", interfaceInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存实体用况
     */
    @RequiresPermissions("interface:interface:edit")
    @Log(title = "实体用况", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(InterfaceInfo interfaceInfo) {
//        InterfaceInfo info = interfaceInfoService.selectInterfaceInfoById(interfaceInfo.getId());
//        if (!info.getAppType().equals(interfaceInfo.getAppType())) {
//            // 查询有没有被其他接口引用
//            VirtualUsage virtualUsage = new VirtualUsage();
//            virtualUsage.setInterfaceId(interfaceInfo.getId());
//            List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsageList(virtualUsage);
//            if (CollectionUtil.isNotEmpty(virtualUsages)) {
//                return AjaxResult.warn("当前接口被其他接口引用，不能修改引用类型");
//            }
//            virtualUsage.setInterfaceId(null);
//            virtualUsage.setRelationInterfaceId(interfaceInfo.getId());
//            virtualUsages = this.virtualUsageService.selectVirtualUsageList(virtualUsage);
//            if (CollectionUtil.isNotEmpty(virtualUsages)) {
//                return AjaxResult.warn("当前接口引用其他接口，不能修改引用类型");
//            }
//        }

        return toAjax(interfaceInfoService.updateInterfaceInfo(interfaceInfo));
    }
    @Resource
    private IInterfaceParamsFieldRelationService interfaceParamsFieldRelationService;

    @Resource
    private IProductVersionObejctService productVersionObejctService;
    /**
     * 删除实体用况
     */
    @RequiresPermissions("interface:interface:remove")
    @Log(title = "实体用况", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(Long.parseLong(ids));
        InterfaceParamsFieldRelation interfaceParamsFieldRelation = new InterfaceParamsFieldRelation();
        interfaceParamsFieldRelation.setInterfaceId(Long.parseLong(ids));
        if (interfaceInfo.getFbType().equals("2")) {
            InterfaceObject interfaceObject = new InterfaceObject();
            interfaceObject.setBffIds(Convert.toLongSet(ids));
            List<InterfaceObject> interfaceObjectList = this.interfaceObjectService.selectInterfaceObjectList(interfaceObject);
            if (CollectionUtil.isNotEmpty(interfaceObjectList)) {
                for (InterfaceObject object : interfaceObjectList) {
                    if (!object.getObjectType().equals("1") && !object.getObjectType().equals("4") && object.getObjectId() != 0) {
                        return AjaxResult.warn("接口和页面、用况，请解除关联");
                    }
                }
            }

            ProductVersionObejct productVersionObejct = new ProductVersionObejct();
            productVersionObejct.setObjectId(Long.parseLong(ids));
            productVersionObejct.setObjectType("5");
            List<ProductVersionObejct> productInfos = this.productVersionObejctService.selectProductVersionObejctList(productVersionObejct);
            if (CollectionUtil.isNotEmpty(productInfos)) {
                return AjaxResult.warn("接口和产品版本有关联，请解除关联");
            }

            VirtualUsage virtualUsage = new VirtualUsage();
            virtualUsage.setRelationInterfaceId(Long.parseLong(ids));
            List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsageList(virtualUsage);
            if (CollectionUtil.isNotEmpty(virtualUsages)) {
                return AjaxResult.warn("bff接口下有虚拟用况，请解除关联");
            }
            interfaceParamsFieldRelation.setType("2");
        } else if (interfaceInfo.getFbType().equals("1")) {
            interfaceParamsFieldRelation.setType("1");
            VirtualUsage virtualUsage = new VirtualUsage();
            virtualUsage.setInterfaceId(Long.parseLong(ids));
            List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsageList(virtualUsage);
            if (CollectionUtil.isNotEmpty(virtualUsages)) {
                return AjaxResult.warn("接口与虚拟用况有关联，请解除关联");
            }
        } else {
            interfaceParamsFieldRelation.setType("4");
            VirtualUsage virtualUsage = new VirtualUsage();
            virtualUsage.setRelationInterfaceId(Long.parseLong(ids));
            List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsageList(virtualUsage);
            if (CollectionUtil.isNotEmpty(virtualUsages)) {
                return AjaxResult.warn("dubbo接口下有虚拟用况，请解除关联");
            }

            VirtualUsage virtualUsage2 = new VirtualUsage();
            virtualUsage2.setInterfaceId(Long.parseLong(ids));
            List<VirtualUsage> virtualUsages2 = this.virtualUsageService.selectVirtualUsageList(virtualUsage2);
            if (CollectionUtil.isNotEmpty(virtualUsages2)) {
                return AjaxResult.warn("dubbo与虚拟用况有关联，请解除关联");
            }
        }
        List<InterfaceParamsFieldRelation> interfaceParamsFieldRelations = this.interfaceParamsFieldRelationService.selectInterfaceParamsFieldRelationList(interfaceParamsFieldRelation);
        if (CollectionUtil.isNotEmpty(interfaceParamsFieldRelations)) {
            return AjaxResult.warn("接口下有字段，请解除关联");
        }
        return toAjax(interfaceInfoService.deleteInterfaceInfoByIds(ids));
    }

    @Resource
    private IVirtualUsageService virtualUsageService;

    @GetMapping("/virtualUsage/{id}")
    public String virtualUsage(@PathVariable("id") Long id, ModelMap mmap) {
        InterfaceInfo interfaceInfo = interfaceInfoService.selectInterfaceInfoById(id);
        mmap.put("interfaceInfo", interfaceInfo);
        VirtualUsage virtualUsage = new VirtualUsage();
        virtualUsage.setRelationInterfaceId(id);
        List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsageList(virtualUsage);
        mmap.put("virtualUsages", virtualUsages);

        InterfaceInfo param = new InterfaceInfo();
        param.setNotId(id);
        List<InterfaceInfo> interfaceInfos = interfaceInfoService.selectInterfaceInfoList(param);
        mmap.put("interfaceDatas", interfaceInfos);
        return prefix + "/virtualUsage";
    }

    @PostMapping("/saveVirtualUsage")
    @ResponseBody
    public AjaxResult saveVirtualUsage(InterfaceInfo interfaceInfo) {
        this.virtualUsageService.saveVirtualUsage(interfaceInfo);
        return success();
    }
    @PostMapping("/checkVUId")
    @ResponseBody
    public AjaxResult checkVUId(@RequestParam String vuIds) {
        List<Long> vuIdList = Convert.toLongList(vuIds);
        List<InterfaceParamsFieldRelation> interfaceParamsFieldRelations = this.interfaceParamsFieldRelationService.selectInterfaceParamsFieldRelationByVUids(vuIdList);
        if (CollectionUtil.isNotEmpty(interfaceParamsFieldRelations)) {
            return AjaxResult.warn("虚拟用况接口下有字段，请解除关联");
        }
        return success();
    }


    @Autowired
    private IInterfaceObjectService interfaceObjectService;

    @Autowired
    private ITableInfoService tableInfoService;

    @GetMapping("/tableList")
    public String tableList(@RequestParam Long id, ModelMap mmap) {
        InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(id);
        mmap.put("interfaceInfo", interfaceInfo);
        if (interfaceInfo.getFbType().equals("1")) {
            // 查询表id
            InterfaceObject interfaceObject = new InterfaceObject();
            interfaceObject.setInterfaceId(interfaceInfo.getId());
            interfaceObject.setObjectType("1");
            List<InterfaceObject> interfaceObjectList = this.interfaceObjectService.selectInterfaceObjectList(interfaceObject);
            if (CollectionUtil.isNotEmpty(interfaceObjectList)) {
                Set<Long> tableIds = interfaceObjectList.stream().map(a -> a.getObjectId()).collect(Collectors.toSet());
                TableInfo tableInfo = new TableInfo();
                tableInfo.setTableIds(tableIds);
                List<TableInfo> tableList = this.tableInfoService.selectTableInfoList(tableInfo);
                mmap.put("tableList", tableList);
            }
        } else {
            // 查询dubbo
            VirtualUsage virtualUsage = new VirtualUsage();
            virtualUsage.setRelationInterfaceId(id);
            List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsageList(virtualUsage);
            if (CollectionUtil.isNotEmpty(virtualUsages)) {
                Set<Long> dubboIds = virtualUsages.stream().map(a -> a.getInterfaceId()).collect(Collectors.toSet());
                // 查询表id
                InterfaceObject interfaceObject = new InterfaceObject();
                interfaceObject.setDubboIds(dubboIds);
                interfaceObject.setObjectType("1");
                List<InterfaceObject> interfaceObjectList = this.interfaceObjectService.selectInterfaceObjectList(interfaceObject);
                if (CollectionUtil.isNotEmpty(interfaceObjectList)) {
                    Set<Long> tableIds = interfaceObjectList.stream().map(a -> a.getObjectId()).collect(Collectors.toSet());
                    TableInfo tableInfo = new TableInfo();
                    tableInfo.setTableIds(tableIds);
                    List<TableInfo> tableList = this.tableInfoService.selectTableInfoList(tableInfo);
                    mmap.put("tableList", tableList);
                }
            }
        }
        return prefix + "/tableList";
    }

    @GetMapping("/usageList")
    public String usageList(@RequestParam Long id, ModelMap mmap) {
        InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(id);
        mmap.put("interfaceInfo", interfaceInfo);

        List<Usage> usageList = this.interfaceInfoService.selectUsageList(Collections.singletonList(id));
        mmap.put("usageList", usageList);
        return prefix + "/usageList";
    }

    @Autowired
    private ITableObjectService tableObjectService;

    @Autowired
    private IBusinessSystemService businessSystemService;

    @GetMapping("/businessSystemList")
    public String businessSystemList(@RequestParam Long id, ModelMap mmap) {
        InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(id);
        mmap.put("interfaceInfo", interfaceInfo);
        // 查询dubbo
        VirtualUsage virtualUsage = new VirtualUsage();
        virtualUsage.setRelationInterfaceId(id);
        List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsageList(virtualUsage);
        if (CollectionUtil.isNotEmpty(virtualUsages)) {
            Set<Long> dubboIds = virtualUsages.stream().map(a -> a.getInterfaceId()).collect(Collectors.toSet());
            // 查询表id
            InterfaceObject interfaceObject = new InterfaceObject();
            interfaceObject.setDubboIds(dubboIds);
            interfaceObject.setObjectType("1");
            List<InterfaceObject> interfaceObjectList = this.interfaceObjectService.selectInterfaceObjectList(interfaceObject);
            if (CollectionUtil.isNotEmpty(interfaceObjectList)) {
                Set<Long> tableIds = interfaceObjectList.stream().map(a -> a.getObjectId()).collect(Collectors.toSet());

                // 查询业务系统id
                TableObject tableObject = new TableObject();
                tableObject.setObjectType("1");
                tableObject.setTableIds(tableIds);
                List<TableObject> tableObjects = this.tableObjectService.selectTableObjectList(tableObject);
                if (CollectionUtil.isNotEmpty(tableObjects)) {
                    Set<Long> businessSystemIds = tableObjects.stream().map(a -> a.getObjectId()).collect(Collectors.toSet());

                    BusinessSystem businessSystem = new BusinessSystem();
                    businessSystem.setBusinessSystemIds(businessSystemIds);
                    List<BusinessSystem> businessSystems = businessSystemService.selectBusinessSystemList(businessSystem);
                    mmap.put("businessSystemList", businessSystems);
                }
            }
        }
        return prefix + "/businessSystemList";
    }

    @Autowired
    private IProductInfoService productInfoService;

    @GetMapping("/productVersionList")
    public String productVersionList(@RequestParam Long id, ModelMap mmap) {
        InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(id);
        mmap.put("interfaceInfo", interfaceInfo);

        List<ProductInfo> productVersionList = this.productInfoService.selectProductVersionList(id);
        mmap.put("productVersionList", productVersionList);
        return prefix + "/productVersionList";
    }

    @GetMapping("/usedInterfaceList")
    public String usedInterfaceList(@RequestParam("id") Long id, ModelMap mmap) {
        List<InterfaceInfo> interfaceInfoList = this.interfaceInfoService.usedInterfaceList(id);
        mmap.put("interfaceList", interfaceInfoList);
        return prefix + "/usedInterfaceList";
    }

    @RequiresPermissions("interface:interface:setEnd")
    @PostMapping("/end")
    @ResponseBody
    public AjaxResult end(Long id) {
        InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(id);
        interfaceInfo.setLifeCycle("9");
        this.interfaceInfoService.updateInterfaceInfo(interfaceInfo);
        return AjaxResult.success();
    }

    @RequiresPermissions("interface:interface:setEnd")
    @PostMapping("/cancelEnd")
    @ResponseBody
    public AjaxResult cancelEnd(Long id) {
        InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(id);
        interfaceInfo.setLifeCycle("8");
        this.interfaceInfoService.updateInterfaceInfo(interfaceInfo);
        return AjaxResult.success();
    }

    @Autowired
    private IInterfaceTestLogService interfaceTestLogService;

    @Resource
    private IInterfaceGeneratorService interfaceGeneratorService;

    @RequiresPermissions("interface:interface:create")
    @PostMapping("/batchUpdate")
    @ResponseBody
    @Transactional(rollbackFor=Exception.class)
    public AjaxResult batchUpdate() {

        Long historyPoolId = -1L;
        Long waiteReleasePoolId = -1L;
        Long cachePoolId = -1L;
        ProductInfo prodinfoQuery = new ProductInfo();
        prodinfoQuery.setProductName("发版历史记录池");
        List<ProductInfo> prodList = productInfoMapper.selectProductInfoList(prodinfoQuery);
        if(CollectionUtil.isNotEmpty(prodList)){
            historyPoolId = prodList.get(0).getId();
        }

        prodinfoQuery.setProductName("待发布接口池");
        prodList = productInfoMapper.selectProductInfoList(prodinfoQuery);
        if(CollectionUtil.isNotEmpty(prodList)){
            waiteReleasePoolId = prodList.get(0).getId();
        }

        prodinfoQuery.setProductName("缓冲池");
        prodList = productInfoMapper.selectProductInfoList(prodinfoQuery);
        if(CollectionUtil.isNotEmpty(prodList)){
            cachePoolId = prodList.get(0).getId();
        }

        if(historyPoolId.longValue()==-1L){
            AjaxResult.error("发版历史记录池未找到，请先进行配置");
        }

        if(waiteReleasePoolId.longValue()==-1L){
            AjaxResult.error("待发布接口池未找到，请先进行配置");
        }

        if(cachePoolId.longValue()==-1L){
            AjaxResult.error("缓冲池未找到，请先进行配置");
        }

        ProductVersionObejct queryVobjectReq = new ProductVersionObejct();
        queryVobjectReq.setTableProductVersionId(historyPoolId);
        List<ProductVersionObejct> historyObjectList = productVersionObejctMapper.selectProductVersionObejctList(queryVobjectReq);
        if(CollectionUtil.isNotEmpty(historyObjectList)){
            List<String> historyIds = historyObjectList.stream().map(h->String.valueOf(h.getId())).collect(Collectors.toList());
            productVersionObejctMapper.deleteProductVersionObejctByIds(historyIds.toArray(new String[0]));
        }

        queryVobjectReq.setTableProductVersionId(waiteReleasePoolId);
        List<ProductVersionObejct> waiteObjectList = productVersionObejctMapper.selectProductVersionObejctList(queryVobjectReq);
        if(CollectionUtil.isNotEmpty(waiteObjectList)){
            List<String> waiteIds = waiteObjectList.stream().map(w->String.valueOf(w.getId())).collect(Collectors.toList());
            for(String one : waiteIds){
                ProductVersionObejct upVobjectReq = new ProductVersionObejct();
                upVobjectReq.setId(Long.valueOf(one));
                upVobjectReq.setTableProductVersionId(historyPoolId);
                productVersionObejctMapper.updateProductVersionObejct(upVobjectReq);
            }
        }

        queryVobjectReq.setTableProductVersionId(cachePoolId);
        List<ProductVersionObejct> cacheObjectList = productVersionObejctMapper.selectProductVersionObejctList(queryVobjectReq);
        if(CollectionUtil.isNotEmpty(cacheObjectList)){
            List<String> cacheIds = cacheObjectList.stream().map(c->String.valueOf(c.getId())).collect(Collectors.toList());
            for(String one : cacheIds){
                ProductVersionObejct upVobjectReq = new ProductVersionObejct();
                upVobjectReq.setId(Long.valueOf(one));
                upVobjectReq.setTableProductVersionId(waiteReleasePoolId);
                productVersionObejctMapper.updateProductVersionObejct(upVobjectReq);
            }
        }

        List<Long> interfaceIds = cacheObjectList.stream().map(co->co.getObjectId()).collect(Collectors.toList());
        interfaceTestLogService.batchUpdateInterface(interfaceIds, "40");
        this.interfaceGeneratorService.generator(interfaceIds);
        return AjaxResult.success();
    }
}
